﻿using PagedList;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framework.Common.Paged
{
    /// <summary>
    /// 自定义分页数据集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CustomPagedList<TTo> //: IEnumerable<TTo>
    {
        public int FirstItemOnPage { get; protected set; }
        public bool HasNextPage { get; protected set; }
        public bool HasPreviousPage { get; protected set; }
        public bool IsFirstPage { get; protected set; }
        public bool IsLastPage { get; protected set; }
        public int LastItemOnPage { get; protected set; }
        /// <summary>
        /// 页总数
        /// </summary>
        public int PageCount { get; protected set; }
        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageNumber { get; protected set; }
        /// <summary>
        /// 每页显示条数
        /// </summary>
        public int PageSize { get; protected set; }
        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalItemCount { get; protected set; }
        /// <summary>
        /// 当前页数据集合
        /// </summary>
        public IEnumerable<TTo> PageData { get; protected set; }
        protected CustomPagedList() { }
        public CustomPagedList(StaticPagedList<TTo> pagedList)
        {
            this.FirstItemOnPage = pagedList.FirstItemOnPage;
            this.HasNextPage = pagedList.HasNextPage;
            this.HasPreviousPage = pagedList.HasPreviousPage;
            this.IsFirstPage = pagedList.IsFirstPage;
            this.IsLastPage = pagedList.IsLastPage;
            this.LastItemOnPage = pagedList.LastItemOnPage;
            this.PageCount = pagedList.PageCount;
            this.PageNumber = pagedList.PageNumber;
            this.PageSize = pagedList.PageSize;
            this.TotalItemCount = pagedList.TotalItemCount;
			this.PageData = pagedList;
        }
        //public static CustomPagedList<TTo> FromIPagedList<TFrom>(IPagedList<TFrom> pagedList, Func<TFrom, TTo> converter)
        //{
        //    return new CustomPagedList<TTo>
        //    {
        //        PageCount = pagedList.PageCount,
        //        PageNumber = pagedList.PageNumber,
        //        PageSize = pagedList.PageSize,
        //        TotalItemCount = pagedList.TotalItemCount,

        //        Data = pagedList.Select(converter).ToList(),
        //    };
        //}
        internal static CustomPagedList<TTo> FromIPagedList<TFrom>(CustomPagedList<TFrom> source, Func<TFrom, TTo> converter)
        {
            return new CustomPagedList<TTo>
            {
                PageCount = source.PageCount,
                PageNumber = source.PageNumber,
                PageSize = source.PageSize,
                TotalItemCount = source.TotalItemCount,
				PageData = source.PageData.Select(converter).ToList(),
            };
        }

        //public IEnumerator<TTo> GetEnumerator()
        //{
        //    foreach (TTo o in Data)
        //    {
        //        // 检查是否到了末尾
        //        if (o == null)
        //        {
        //            break;
        //        }

        //        // 返回当前元素。然后前进一步
        //        yield return o;
        //    }
        //}

        //IEnumerator IEnumerable.GetEnumerator()
        //{
        //    foreach (object o in Data)
        //    {
        //        // 检查是否到了末尾
        //        if (o == null)
        //        {
        //            break;
        //        }

        //        // 返回当前元素。然后前进一步
        //        yield return o;
        //    }
        //}
    }
}
